package org.happen.android.module;

import org.liushui.mycommons.android.annotation.ViewInject;
import org.liushui.mycommons.android.annotation.helper.InjectHelper;
import org.liushui.mycommons.android.util.McViewUtil;

import android.app.Activity;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.happen.android.R;

public class TitleModule
{

	Activity activity;

	@ViewInject(id = R.id.title)
	public View container;
	@ViewInject(id = R.id.btn_title1)
	public ImageView btnBack;
	@ViewInject(id = R.id.btn_title2)
	public ImageView btnEdit;
	@ViewInject(id = R.id.tv_title)
	public TextView tvTitle;
	@ViewInject(id = R.id.rellayout)
	public RelativeLayout layout;

	public TitleModule(Activity activity)
	{
		this(activity, "");
	}

	public TitleModule(Activity activity, View view)
	{
		this(activity, view, true, false, "");
	}

	public TitleModule(Activity activity, String title)
	{
		this(activity, true, false, title);
	}

	public TitleModule(Activity activity, int titleRes)
	{
		this(activity, true, false, "");
		setTitle(titleRes);
	}

	public TitleModule(Activity activity, boolean showBack, boolean showEdit, String title)
	{
		this.activity = activity;
		InjectHelper.init(this, activity);
		showBack(showBack);
		showEdit(showEdit);
		setTitle(title);

		if (btnBack != null)
		{
			btnBack.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v)
				{
					if (v == btnBack)
					{
						TitleModule.this.activity.finish();
					}
				}
			});
		}
	}

	public TitleModule(Activity activity, View view, boolean showBack, boolean showEdit, String title)
	{
		this.activity = activity;
		if (view == null)
		{
			InjectHelper.init(this, activity);
		} else
		{
			InjectHelper.init(this, view);
		}
		showBack(showBack);
		showEdit(showEdit);
		setTitle(title);

		if (btnBack != null)
		{
			btnBack.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v)
				{
					if (v == btnBack)
					{
						TitleModule.this.activity.finish();
					}
				}
			});
		}
	}

	public void setTitle(String s)
	{
		tvTitle.setText(s);
	}

	public void setTitle(int resId)
	{
		tvTitle.setText(resId);
	}

	public void showBack(boolean show)
	{
		McViewUtil.showORHiden(btnBack, show);
	}

	public void showEdit(boolean show)
	{
		McViewUtil.showORHiden(btnEdit, show);
	}

	public void setEditText(int resId)
	{
		setEditText(activity.getString(resId));
	}

	public void setEditText(CharSequence text)
	{
		// btnEdit.setImageBitmap(UtilImage.makeTextImageViewBitmap(btnEdit, text));
		// btnEdit.setText(text);
	}

	public void setEditDrawable(int resId)
	{
		SpannableString ss = new SpannableString(" ");
		ImageSpan imageSpan = new ImageSpan(activity, resId);
		ss.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		setEditText(ss);
	}

	public void setEdit2Text(int resId)
	{
		setEdit2Text(activity.getString(resId));
	}

	public void setEdit2Text(CharSequence text)
	{
		// btnEdit2.setImageBitmap(UtilImage.makeTextImageViewBitmap(btnEdit2, text));
		// btnEdit2.setText(text);
	}

	public void setEdit2Drawable(int resId)
	{
		SpannableString ss = new SpannableString(" ");
		ImageSpan imageSpan = new ImageSpan(activity, resId);
		ss.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		setEdit2Text(ss);
	}

	public void setBackClickListener(OnClickListener clickListener)
	{
		btnBack.setOnClickListener(clickListener);
	}
}